



package top.gongtao.helloservice;

import com.netflix.discovery.converters.Auto;
import com.sun.jndi.toolkit.url.Uri;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import freemarker.log.Logger;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

@RestController
public class ConsumerController {

	@Autowired
	private RestTemplate restTemplate;

	@RequestMapping("/ribbon-consumer")
	public String index(){
		return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
	}

	@RequestMapping("/ribbon-consumer2")
	public String index2(){

		ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class);
		String result = responseEntity.getBody();
		String result2 = restTemplate.getForObject("http://HELLO-SERVICE/hello",String.class);
		UriComponents components = UriComponentsBuilder.fromUriString("http://HELLO-SERVICE/hello")
				.build()
				.expand("dodo")
				.encode();
		URI uri = components.toUri();
		String result3 = restTemplate.getForObject(uri,String.class);

		User user = new User("gongtao",23);
		ResponseEntity<String> responseEntity1 = restTemplate.postForEntity("http://HELLO-SERVICE/hello",user,String.class);
		String result4 = responseEntity1.getBody();


		//第三种 posrForLocation 该方法实现了以POST请求提交资源，并返回新的资源的 URI
		URI uri1 = restTemplate.postForLocation("http://HELLO-SERVICE/hello",user);

		// GET , POST , PUT , DELETE


		// 在 RestTemplate 使用 PUT 请求，可以通过 put 方法进行调用实现，put 函数为 void 类型，所以没有返回内容，也就没有其他函数
		// 定义的 responseType 参数 比如
		Long id = 10001L;
		User user2 = new User("龚涛",23);
		restTemplate.put("http://USER-SERVICE/user/{1}",user2,id);

		// DELETE 请求，在 RestTemplate 中 对 DELETE 请求可以通过  delete方法来实现，比如
		Long id2 = 10001L;
		restTemplate.delete("http://USER-SERVICE/user/{1}",id2);


		return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
	}
}